import os

def curl(api, host='http://127.0.0.1:5000/', user=None, pwd=None, token=None, get=True):
    cmd = "curl -i '%s%s'" % (host, api)
    if user:
        if pwd:
            cmd += " -u '%s:%s'" % (user,pwd)
        else:
            cmd += " -u '%s'" % user
    if token:
        cmd += " -H 'Authorization: Bearer %s'" % token
    if get:
        cmd += " -X GET"
    else:
        cmd += " -X POST"
    os.system(cmd)

def test(get=True, host='http://127.0.0.1:5000/', user='susan', pwd='bye'):
    def call(api, token=None, user=None, pwd=None):
        print(api)
        curl(api, host=host, user=user, pwd=pwd, token=token, get=get)
    #
    def tcall(api, rftoken, atoken):
        print('-------------------------')
        print('---------------')
        print('1 : password test')
        call(api, user=user, pwd=pwd)
        #
        print('---------------')
        print('2: refresh token test')
        call(api, token=rftoken)
        #
        print('---------------')
        print('3: access token test')
        call(api, token=atoken)
    #-----------#
    call('token', user=user, pwd=pwd)
    rftoken = raw_input('input refresh_token: ')
    atoken = raw_input('input access_token: ')
    #-----------#
    tcall('refresh_token', rftoken, atoken)
    tcall('access_token', rftoken, atoken)
    tcall('token', rftoken, atoken)
    tcall('need_pwd', rftoken, atoken)
    tcall('need_rftoken', rftoken, atoken)
    tcall('need_pwd_rftoken', rftoken, atoken)
    tcall('need_atoken', rftoken, atoken)
    tcall('need_pwd_atoken', rftoken, atoken)
    #-----------#
    